
import numpy as np

def calculate():
    # 城市名数组
    city_name = []
    # 城市坐标数组
    city_condition = []
    # 通过open的方法打开txt文件输入数据
    with open('data.txt', 'r', encoding='utf-8') as f:
        lines = f.readlines()  # 读取每一行数据
        # 处理每一行数据
        for line in lines:
            line = line.split('\n')[0]
            # 数据通过空格和逗号分离出坐标x和坐标y以及城市名称
            line = line.split(',')
            # print(line)
            # 将分离出的城市名和城市地址放入数组中
            city_name.append(line[0])
            city_condition.append([float(line[1]), float(line[2])])
    # 因为格式原因而转换成numpy数组
    city_condition = np.array(city_condition)

    # 使用短变量
    _cnt = len(city_name)
    _cdt = city_condition

    # 生成矩阵
    _matr = np.zeros([_cnt, _cnt])

    for i in range(_cnt):
        for j in range(i, _cnt):
            if i == j:
                _matr[i][j] = 0
            else:
                x2 = abs(_cdt[i][0] - _cdt[j][0])
                y2 = abs(_cdt[i][1] - _cdt[j][1])
                _matr[j][i] = _matr[i][j] = x2 + y2

    mat = np.array(_matr)
    return mat
